package coroutines.coroutine.supervisorscope

import kotlinx.coroutines.*

class SupervisorScopeTest {
}

fun main() {
//    // GlobalScope的子协程抛出异常，这个协程自己和其他子协程也会终止
////    GlobalScope.launch(context = Dispatchers.IO) {
////        println("GlobalScope.launch() start0")
////
////        launch {
////            println("GlobalScope.launch() launch1 start")
////            delay(100)
////            println("GlobalScope.launch() launch1 completed")
////        }
////
////        launch {
////            println("GlobalScope.launch() launch2 start")
////            delay(200)
////            println("GlobalScope.launch() launch2 completed")
////
////            throw Exception("Custom Exception")
////        }
////
////        launch {
////            println("GlobalScope.launch() launch3 start")
////            delay(300)
////            println("GlobalScope.launch() launch3 completed")
////        }
////
////        delay(400)
////
////        println("GlobalScope.launch() start1")
////    }
//
//    // runBlocking 的子协程抛出异常，这个协程自己和其他子协程也会终止
////    runBlocking(context = Dispatchers.IO) {
////        println("runBlocking start0")
//////
//////        launch {
//////            println("runBlocking launch1 start")
//////            delay(100)
//////            println("runBlocking launch1 completed")
//////        }
//////
//////        launch {
//////            println("runBlocking launch2 start")
//////            delay(200)
//////            println("runBlocking launch2 completed")
//////
//////            throw Exception("Custom Exception")
//////        }
//////
//////        launch {
//////            println("runBlocking launch3 start")
//////            delay(300)
//////            println("runBlocking launch3 completed")
//////        }
//////
////        delay(500)
////
////        println("runBlocking start1")
////    }
    runBlocking {
        println("runBlocking start0")

        supervisorScope {
            println("supervisorScope start0")

            launch {
                println("supervisorScope launch1 start")
                delay(200)
                println("supervisorScope launch1 before throw Exception")
                throw Exception("Custom Exception")
                println("supervisorScope launch1 after throw Exception")
            }

            launch {
                println("supervisorScope launch2 start")
                delay(100)
                println("supervisorScope launch2 complete")
            }

            launch {
                println("supervisorScope launch3 start")
                delay(300)
                println("supervisorScope launch3 complete")
            }

            println("supervisorScope start1")
        }

        println("runBlocking start1")
    }
}

//fun main()  = runBlocking {
//    println("runBlocking start0")
//
//    supervisorScope {
//        println("supervisorScope start0")
//
//        launch {
//            println("supervisorScope launch1 start")
//            delay(200)
//            println("supervisorScope launch1 before throw Exception")
//            throw Exception("Custom Exception")
//            println("supervisorScope launch1 after throw Exception")
//        }
//
//        launch {
//            println("supervisorScope launch2 start")
//            delay(100)
//            println("supervisorScope launch2 complete")
//        }
//
//        launch {
//            println("supervisorScope launch3 start")
//            delay(300)
//            println("supervisorScope launch3 complete")
//        }
//
//        println("supervisorScope start1")
//    }
//
//    println("runBlocking start1")
//}